import http from "../../../utils/http"
import util from "../../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    withdrawMoney:'',
    withdrawType: '',
    optionData:{},
    columns: [
      { text: '微信', value: 'wechat' },
      { text: '支付宝', value: 'alipay' },
      { text: '银行卡', value: 'bank' },
    ],
    show: false,
    columnsActiveIndex: 0,
    withdrawInfo: {}, // 提现方式信息-公用
    showInfoFlag: false,
  },

  submit(){
    if (this.data.withdrawMoney === '') return util.toast('请输入提现金额')
    if (this.data.withdrawType === '') return util.toast('请选择提现方式')

    http.post('/addons/shopro/user_wallet_apply/apply',{
      money:  this.data.withdrawMoney,
      type:  this.data.withdrawType,
    }, true).then(res => {
      util.toast(res.msg)
      setTimeout(()=>{
        util.back();
      }, 1000)
    })
  },
  onSubmitInfo(event){
    let errorMsg = '请输入';
    const { real_name,card_no,bank_name } = event.detail.value;
    if (!real_name) {
      this.setData({
        ['tips.real_name']: errorMsg
      });
      return;
    }
    if (!bank_name && this.data.withdrawType === 'bank') {
      this.setData({
        ['tips.bank_name']: errorMsg
      });
      return;
    }
    if (!card_no) {
      this.setData({
        ['tips.card_no']: errorMsg
      });
      return;
    }

    errorMsg = ''
    this.setData({
      ['tips.real_name']: errorMsg,
      ['tips.bank_name']: errorMsg,
      ['tips.card_no']: errorMsg
    });

    // 所有表单项验证通过后的操作
    const params = {
      real_name,
      card_no,
      type: this.data.withdrawType,
    }
    if(this.data.withdrawType === 'bank') params['bank_name'] = bank_name;

    http.post('/addons/shopro/user_bank/edit',params, true).then(res => {
      util.toast('添加成功')
      this.setData({
        withdrawInfo: res.data
      })
      this.closeShowInfoPopup()
    })
  },
  addWithdrawInfoTap(){
    this.setData({
      showInfoFlag: true,
    })
  },
  closeShowInfoPopup(){
    this.setData({
      showInfoFlag: false,
    })
  },
  onConfirm(event) {
    const { value, index } = event.detail;
    this.setData({
      columnsActiveIndex: index,
      withdrawType: value.value
    })
    this.onCancel()

    if(value.value === 'wechat'){
      // 微信提现
      this.getWeChatInfo();
    }else if(this.data.withdrawType === 'alipay'){
      // 支付宝提现
      this.getAlipayOrBankInfo(this.data.withdrawType);
    }else if(this.data.withdrawType === 'bank'){
      // 银行卡提现
      this.getAlipayOrBankInfo(this.data.withdrawType)
    }
  },
  onCancel() {
    this.setData({
      show: false
    })
  },
  onSelectStyle(){
    this.setData({
      show: true
    })
  },
  withdrawRecordTap(){
    util.skip('/service/distribution/withdrawRecord/withdrawRecord')
  },
  setWithdrawMoney(e){
    this.setData({
      withdrawMoney: e.detail.value
    })
  },
  getAlipayOrBankInfo(type){
    this.setData({
      withdrawInfo: {}
    })
    http.get('/addons/shopro/user_bank/info',{
      type: type,
    }, true).then(res => {
      this.setData({
        withdrawInfo: res.data
      })
    })
  },
  getWeChatInfo(){
    this.setData({
      withdrawInfo: {}
    })
    http.get('/addons/shopro/user_bank/info',{
      type:  'wechat',
    }, true).then(res => {
      this.setData({
        withdrawInfo: res.data
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      withdrawMoney:'',
      withdrawType: '',
      optionData: options
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})